// file.js
import {
	baseUrl
} from '../config'; // 假设有基础URL配置

/**
 * 上传文件到服务器
 * @param {string} filePath 文件临时路径
 * @returns {Promise<string>} 文件URL
 */
export const uploadFile = (filePath) => {
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: `${baseUrl}/elderly/file/upload`,
			filePath: filePath,
			name: 'file',
			success: (res) => {
				if (res.statusCode === 200) {
					const data = JSON.parse(res.data);
					if (data && data.code === 1000) {
						resolve(data.data); // 返回图片URL
					} else {
						reject(new Error(data.msg || '上传失败'));
					}
				} else {
					reject(new Error(`上传失败，状态码: ${res.statusCode}`));
				}
			},
			fail: (err) => {
				reject(err);
			}
		});
	});
};

/**
 * 批量上传文件
 * @param {string[]} filePaths 文件临时路径数组
 * @returns {Promise<string[]>} 文件URL数组
 */
export const batchUploadFiles = async (filePaths) => {
	const uploadPromises = filePaths.map(filePath => uploadFile(filePath));
	return Promise.all(uploadPromises);
};